import { plugin} from '../../api/api.js';
import { Write_player,Do,existplayer,
    Read_player} from '../../model/xiuxian.js';
import fs from 'fs';
              
let moveTimeout = null

    export class move extends plugin {
        constructor() {
          super({
            name: 'Userrun',
            dsc: '跑图模块',
            event: 'message',
            rule: [
              {
                reg: /^前往.*$/,
                fnc: 'go',
              },
              {
                reg: '^返回$',
                fnc: 'back',
              },
              {
                reg: '^我的位置$',
                fnc: 'myLocation',
              }
              
              
            ],
          });
        }
        async back(e) {
            let usr_qq = e.user_id.toString().replace('qg_',''); // 获取用户QQ
        //有无账号
         let ifexistplay = await existplayer(usr_qq);
         if (!ifexistplay) return false;
         
          
            if (moveTimeout) {
                clearTimeout(moveTimeout); // 取消移动
                moveTimeout = null;
                e.reply("已取消移动，坐标未改变。");
            } else {
                e.reply("当前没有进行中的移动。");
            }
            
        }
        

        async go(e) {
            const target = e.msg.slice(2); // 提取目标地点名，例如："前往圣魂村"
            let usr_qq = e.user_id.toString().replace('qg_','');; // 获取用户QQ
            
        //有无账号
         let ifexistplay = await existplayer(usr_qq);
         if (!ifexistplay) return false;
         let player = await Read_player(usr_qq); // 读取玩家数据
         if (player.当前血量 <= 100) {
          e.reply(`重伤在地动弹不得`);
          return false;
        }
        let flag = await Do(e);
        if (!flag) {
        return false;
      }

            // 获取目标位置
            const positions = JSON.parse(fs.readFileSync("plugins/xiuxian-lh/resources/data/map/position.json", "utf8"));
            const targetPosition = positions.find(p => p.name === target);

            if (!targetPosition) {
                e.reply("目标地点不存在！");
                return;
            }

            // 当前坐标与目标坐标
            const { x: currX, y: currY } = player;
            const targetX = (parseInt(targetPosition.x1) + parseInt(targetPosition.x2)) / 2;
            const targetY = (parseInt(targetPosition.y1) + parseInt(targetPosition.y2)) / 2;

            // 计算距离
            const distance = Math.sqrt(Math.pow(targetX - currX, 2) + Math.pow(targetY - currY, 2));

            // 移动速度
            const speed = player.速度 ; // 

            // 缩放因子，确保最远两点移动时间不超过3分钟
            const scaleFactor = 6 / Math.sqrt(Math.pow(1200 - 200, 2) + Math.pow(1200 - 100, 2));

            // 所需时间
            const travelTime = (distance * scaleFactor) / speed;
            const travelTimeMinutes = Math.floor(travelTime); // 分钟部分
            const travelTimeSeconds = Math.round((travelTime - travelTimeMinutes) * 60); // 秒部分

            // 检查是否已有移动在进行
            if (moveTimeout) {
                e.reply("当前正在移动中，请等待移动完成后再前往新的地方。");
                return;
            }

            e.reply(`前往${target}需要${travelTimeMinutes}分${travelTimeSeconds}秒，开始移动...`);

            // 更新坐标
            moveTimeout = setTimeout(async () => {
                player.x = targetX;
                player.y = targetY;
                await Write_player(usr_qq, player);
                e.reply(`已到达${target}！`);
                moveTimeout = null; // 移动完成后清除timeout ID
            }, (travelTimeMinutes * 60 + travelTimeSeconds) * 1000); // 延迟模拟移动时间
        }
        async myLocation(e) {
            let usr_qq = e.user_id.toString().replace('qg_',''); // 获取用户QQ
            let ifexistplay = await existplayer(usr_qq);
         if (!ifexistplay) return false;
            const player = await Read_player(usr_qq); // 读取玩家数据

            // 获取玩家当前坐标
            const { x: currX, y: currY } = player;

            // 获取地图位置
            const positions = JSON.parse(fs.readFileSync("plugins/xiuxian-lh/resources/data/map/position.json", "utf8"));

            // 找到玩家所在位置
            const currentPosition = positions.find(p => 
                currX >= parseInt(p.x1) && currX <= parseInt(p.x2) &&
                currY >= parseInt(p.y1) && currY <= parseInt(p.y2)
            );

            if (currentPosition) {
                e.reply(`你当前的位置是：${currentPosition.name}`);
            } else {
                e.reply("当前你的位置未知！");
            }
        }




    }